package com.wskj.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.system.bo.SysBannerAddBo;
import com.wskj.system.bo.SysBannerEditBo;
import com.wskj.system.bo.SysBannerQueryBo;
import com.wskj.system.domain.SysBanner;
import com.wskj.system.mapper.SysBannerMapper;
import com.wskj.system.service.ISysBannerService;
import com.wskj.system.vo.web.SysBannerVo;
import org.springframework.stereotype.Service;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 轮播图Service业务层处理
 *
 * @author wsdz
 * @date 2023-03-02
 */
@Service
public class SysBannerServiceImpl extends ServicePlusImpl<SysBannerMapper, SysBanner> implements ISysBannerService {

    @Override
    public SysBannerVo queryById(Long bannerId){
        return getVoById(bannerId, SysBannerVo.class);
    }

    @Override
    public TableDataInfo<SysBannerVo> queryPageList(SysBannerQueryBo bo) {
        PagePlus<SysBanner, SysBannerVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysBannerVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysBannerVo> queryList(SysBannerQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysBannerVo.class);
    }

    private LambdaQueryWrapper<SysBanner> buildQueryWrapper(SysBannerQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysBanner> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getBannerTitle()), SysBanner::getBannerTitle, bo.getBannerTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getBannerPic()), SysBanner::getBannerPic, bo.getBannerPic());
        lqw.eq(StrUtil.isNotBlank(bo.getBannerDesc()), SysBanner::getBannerDesc, bo.getBannerDesc());
        lqw.eq(bo.getSort() != null, SysBanner::getSort, bo.getSort());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), SysBanner::getStatus, bo.getStatus());
        lqw.eq(bo.getReleaseTime() != null, SysBanner::getReleaseTime, bo.getReleaseTime());
        lqw.eq(bo.getBannerClass() != null, SysBanner::getBannerClass, bo.getBannerClass());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysBannerAddBo bo) {
        SysBanner add = BeanUtil.toBean(bo, SysBanner.class);
        validEntityBeforeSave(add);
        if(bo.getStatus() != null && bo.getStatus().equals("0")){
            add.setReleaseTime(new Date());
        }
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysBannerEditBo bo) {
        SysBanner update = BeanUtil.toBean(bo, SysBanner.class);
        validEntityBeforeSave(update);
        if(bo.getStatus() != null && bo.getStatus().equals("0")){
            update.setReleaseTime(new Date());
        }
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysBanner entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importSysBanner(List<SysBannerVo> sysBannerList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(sysBannerList), "导入数据不能为空");
        int successNum = 0;
        for (SysBannerVo sysBanner : sysBannerList) {
            SysBanner add = BeanUtil.toBean(sysBanner, SysBanner.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", sysBannerList.size(), successNum);
        if (successNum != sysBannerList.size()) {
            msg = msg + String.format("失败 %d 条！", sysBannerList.size() - successNum);
        }
        return msg;
    }
}
